/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.SPI;

import java.util.Arrays;
import java.util.Objects;

/**
 * @description: AbstractLoadStrategy
 * @author: zzh
 * @date: 2021/9/23 09:36
 */
public abstract class AbstractLoadStrategy implements LoadStrategy {

  private String directory;

  private String[] excludedPackages;

  private int priority;

  private boolean overridden;

  private boolean preferExtensionClassLoader;

  public AbstractLoadStrategy() {
    this.directory = directory();
    this.excludedPackages = excludedPackages();
    this.priority = getPriority();
    this.overridden = overridden();
    this.preferExtensionClassLoader = preferSpiClassLoader();
  }

  public void setDirectory(String directory) {
    this.directory = directory;
  }

  public void setExcludedPackages(String[] excludedPackages) {
    this.excludedPackages = excludedPackages;
  }

  public void setPriority(int priority) {
    this.priority = priority;
  }

  public void setOverridden(boolean overridden) {
    this.overridden = overridden;
  }

  public void setPreferExtensionClassLoader(boolean preferExtensionClassLoader) {
    this.preferExtensionClassLoader = preferExtensionClassLoader;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    AbstractLoadStrategy that = (AbstractLoadStrategy) o;
    return priority == that.priority
        && overridden == that.overridden
        && preferExtensionClassLoader == that.preferExtensionClassLoader
        && Objects.equals(directory, that.directory)
        && Arrays.equals(excludedPackages, that.excludedPackages);
  }

  @Override
  public int hashCode() {
    int result = Objects.hash(directory, priority, overridden, preferExtensionClassLoader);
    result = 31 * result + Arrays.hashCode(excludedPackages);
    return result;
  }

  @Override
  public String toString() {
    return "AbstractLoadStrategy{"
        + "directory='"
        + directory
        + '\''
        + ", excludedPackages="
        + Arrays.toString(excludedPackages)
        + ", priority="
        + priority
        + ", overridden="
        + overridden
        + ", preferExtensionClassLoader="
        + preferExtensionClassLoader
        + '}';
  }
}
